<?php
class ContactPersonsController extends AppController{

	var $uses = array('ContactPerson');

	function beforeFilter(){
		parent::beforeFilter();
	}

	/*
	 * standard method
	 */
	public function index(){
		$params = array();
		if(!empty($this->params['form'])
		&& (strlen($this->params['form']['findby_company_name'])>0
			|| strlen($this->params['form']['findby_name'])>0)
		){
			$params = array(
				'conditions' => array(
					'Company.company_name LIKE' => '%'.$this->params['form']['findby_company_name'].'%',
					'CONCAT(ContactPerson.first_name," ",ContactPerson.family_name) LIKE' => '%'.$this->params['form']['findby_name'].'%'
				)
			);
		}else{
			$this->params['form']['findby_company_name']='';
			$this->params['form']['findby_name']='';
		}

		$this->data = $this->ContactPerson->find('all',$params);

	}

	/**
	 * Add a contact person
	 */
	function add(){
		if(!empty($this->data)){
			$ok = $this->ContactPerson->save($this->data);
			$trueMsg = 'Kontakt wurde angelegt';
			$falseMsg = 'Der Kontakt konnte nicht angelegt werden. '. $this->Session->error();
			parent::doFlash($ok, $trueMsg, $falseMsg);
			if($ok){
				$this->redirect('/manager/ContactPersons/index');
			}
		}

		// get companies  ---------------------
		$params = array(
			'fields' => array('Company.id','Company.company_name')
		);
		$comps = $this->ContactPerson->Company->find('list',$params);
		$this->set('companies',$comps);
	}

	/**
	 * update a contact person 
	 */
	function update(){


	}

}
?>
